package com.fs.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.fs.entity.Receipt;
import com.fs.utils.DBUtils;
public class ReceiptDao {

	public void save(Receipt receipt) {
		String sql="insert into tb_receipt(user_id,receipt_name,receipt_phone,receipt_addr) values(?,?,?,?)";		
		Connection con=null;
		PreparedStatement ps=null;
		
		try {
		con=DBUtils.getConnection();
		ps=con.prepareStatement(sql);
		ps.setInt(1,receipt.getUser_id());
		ps.setString(2,receipt.getReceipt_name());
		ps.setString(3,receipt.getReceipt_phone());
		ps.setString(4, receipt.getReceipt_addr());
		ps.executeUpdate();
		
	} catch (Exception e) {
		e.printStackTrace();
	}finally{
		DBUtils.close(con, null, null, ps);
	}
	
	}

	/*public Receipt queryreceipt_id(int receipt_id) {
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		boolean temp=false;
		Receipt receipt=new Receipt();
		String sql="select * from tb_receipt where receipt_id=?";	
		try {
			con=DBUtils.getConnection();//获取数据库的连接
			ps=con.prepareStatement(sql);
			ps.setInt(1, receipt_id);
			rs=ps.executeQuery();
			while(rs.next()){
				receipt.setReceipt_id(rs.getInt("receipt_id"));
				receipt.setReceipt_name(rs.getString("receipt_name"));
				receipt.setReceipt_phone(rs.getString("receipt_phone"));
				receipt.setReceipt_addr(rs.getString("addr"));
			}
		
	} catch (Exception e) {
		e.printStackTrace();
	}finally{
		DBUtils.close(con, rs, null, ps);
	}
	return receipt;
	
	}*/

	

	public List<Receipt> getReceiptList(int user_id) {
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		List<Receipt> list=new ArrayList<Receipt>();
		String sql="select * from tb_receipt where user_id=?";	
		try {
			con=DBUtils.getConnection();//获取数据库的连接
			ps=con.prepareStatement(sql);
			ps.setInt(1, user_id);
			rs=ps.executeQuery();
			Receipt receipt=null;
			
			while(rs.next()){
				receipt=new Receipt();
				receipt.setUser_id(rs.getInt("user_id"));
				receipt.setReceipt_id(rs.getInt("receipt_id"));
				receipt.setReceipt_name(rs.getString("receipt_name"));
				receipt.setReceipt_phone(rs.getString("receipt_phone"));
				receipt.setReceipt_addr(rs.getString("receipt_addr"));
				list.add(receipt);
			}
		
	} catch (Exception e) {
		e.printStackTrace();
	}finally{
		DBUtils.close(con, rs, null, ps);
	}
	return list;
	}

	public Receipt getReceiptById(int user_id) {
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		Receipt receipt = new Receipt();
	
		String sql="select * from tb_receipt where user_id=?";	
		try {
			con=DBUtils.getConnection();//获取数据库的连接
			ps=con.prepareStatement(sql);
			ps.setInt(1, user_id);
			rs=ps.executeQuery();
			while(rs.next()){
				receipt=new Receipt();
				receipt.setReceipt_id(rs.getInt("user_id"));
				receipt.setReceipt_id(rs.getInt("receipt_id"));
				receipt.setReceipt_name(rs.getString("receipt_name"));
				receipt.setReceipt_phone(rs.getString("receipt_phone"));
				receipt.setReceipt_addr(rs.getString("receipt_addr"));
				
			}
		
	} catch (Exception e) {
		e.printStackTrace();
	}finally{
		DBUtils.close(con, rs, null, ps);
	}
	return receipt;
	}

	public void delete(Integer receipt_id) {
		String sql="delete from tb_receipt where receipt_id=?";
		Connection con=null;
		PreparedStatement ps=null;
		
		try {
			con=DBUtils.getConnection();//获取数据库的连接
			ps=con.prepareStatement(sql);
			ps.setInt(1,receipt_id);
			ps.executeUpdate();
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBUtils.close(con, null, null, ps);
		}
		
	}

	
	
	

}
